package com.ysd.twocrm.echarts;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysd.twocrm.entity.Leavetable;
import com.ysd.twocrm.entity.Userchecks;
import com.ysd.twocrm.entity.Users;
import com.ysd.twocrm.mapper.LeavetableMapper;
import com.ysd.twocrm.mapper.UserChecksMapper;
import com.ysd.twocrm.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class EchartsImpl {
    @Autowired
    private UserChecksMapper userChecksMapper;
    @Autowired
    private LeavetableMapper leavetableMapper;
    @Autowired
    private UsersMapper usersMapper;

    /*
     * @Description 封装饼状图，取到咨询师下的迟到、请假、加班的人数，最后封装成map返回出去
     * @param
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/6/6 14:04
     */

    public  Object echarts4() throws ParseException {

        Integer jiabansum=0;
        Integer kuanggongsum=0;
        Integer qingjiasum=0;
        EchartsCheck e=new EchartsCheck();//echarts实体
        Echartsvaluename echartsvaluename=new Echartsvaluename();//值
        List<Users> zixunshi = usersMapper.findAllUsers("咨询师");
        for(Users i:zixunshi){
            //加班
            Integer jiabanvalue=jiaban(i.getUserName(),i.getUserId());
            if(jiabanvalue>0){
                jiabansum++;
            }
            //请假
            List<Leavetable> leavetables = leavetables(i.getUserName(), i.getUserId());
            if(leavetables.size()>0){
                qingjiasum++;
            }

            //旷工
            Integer integer = echarts1(i.getUserName(), i.getUserId());
            if(integer>0){
                kuanggongsum++;
            }
        }
        String[] center={"50%","60%"};
        List<Echartsvaluename> echartsvaluenameList=new ArrayList<>();
        List<String> dataname=new ArrayList<>();
        echartsvaluename=new Echartsvaluename(jiabansum,"加班人数");
        dataname.add("加班人数");
        echartsvaluenameList.add(echartsvaluename);
        echartsvaluename=new Echartsvaluename(qingjiasum,"请假人数");
        dataname.add("请假人数");
        echartsvaluenameList.add(echartsvaluename);
        echartsvaluename=new Echartsvaluename(kuanggongsum,"旷工人数");
        dataname.add("旷工人数");
        echartsvaluenameList.add(echartsvaluename);
        e.setData(echartsvaluenameList);
        e.setName("考勤访问");
        e.setType("pie");
        e.setCenter(center);
        e.setRedius("55%");
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("data",dataname);
        map.put("series",e);
        return map;
    }


    /*
     * @Description 拿到旷工的人数，从咨询师里查这个月他们都有几个人旷工了，
     * 注意要拿旷工的日期在请假表里找下，如果请假表里有那么就不算旷工
     * @param name
     * @param id
     * @return java.lang.Integer
     * @author liusupeng
     * @date 2020/6/6 13:42
     */
    //旷工的人数
    Integer echarts1(String name, Integer id) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<Userchecks> userchecks = userchecksList(name, id);
        List<Date> dateList = new ArrayList<>();
        List<Leavetable> leavetables = leavetables(name, id);
        try {
            for (Userchecks u : userchecks) {
                dateList.add(sdf.parse(sdf.format(u.getUserCheckInTime())));
            }
            List<Date> timeday = timeday(dateList);
            for (int i = 0; i < timeday.size(); i++) {
                for (int j = 0; j < leavetables.size(); j++) {
                    if (sdf.format(timeday.get(i)).equals(sdf.format(leavetables.get(j).getLsubmit()))) {
                        timeday.remove(i);
                    }
                }
                //   System.out.println(sdf.format(timeday.get(i)));
            }
            return timeday.size();

        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }

    }
    /*
     * @Description 拿到下班的日期去判断是否是在下午五点以后打卡的，如果是的话那么就算他加班，最后返回多少个人加班了
     * @param name
     * @param id
     * @return java.lang.Integer
     * @author liusupeng
     * @date 2020/6/6 13:43
     */
    //加班的人数
    Integer jiaban(String name, Integer id) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Userchecks> userchecksList = userchecksList(name, id);
        for (Userchecks uc : userchecksList) {
            Calendar c = Calendar.getInstance();
            c.setTime(uc.getUserCheckInTime());

            String format = sdf.format(c.getTime());//当前打卡的年月日
            String checkin = format + " 08:00:00";//打卡上班时间设置8点
            String checkout = format + " 17:00:00";//打卡下班时间设置17点
            if (uc.getUserCheckOutTime().getTime() > sdf1.parse(checkout).getTime()) {
                //只要加班了就搞
                return 1;
            }
        }
        return 0;
    }
    /*
     * @Description 请假的数据，查询用户上月是否请假
     * @param name
     * @param id
     * @return java.util.List<com.ysd.twocrm.entity.Leavetable>
     * @author liusupeng
     * @date 2020/6/6 13:44
     */
    //取到请假的数据
    List<Leavetable> leavetables(String name, Integer id) {


        QueryWrapper<Leavetable> leavetableQueryWrapper = new QueryWrapper<Leavetable>();
        leavetableQueryWrapper.eq("lstatus", 0).eq("userId", id).eq("lname", name).likeRight("lsubmit", lastmonth());
        List<Leavetable> leavetables = leavetableMapper.selectList(leavetableQueryWrapper);
        return leavetables;
    }
    /*
     * @Description 取到上个月打卡时间
     * @param name
     * @param id
     * @return java.util.List<com.ysd.twocrm.entity.Userchecks>
     * @author liusupeng
     * @date 2020/6/6 13:44
     */
    //取到打卡的数据
    List<Userchecks> userchecksList(String name, Integer id) {

        QueryWrapper<Userchecks> userchecksQueryWrapper = new QueryWrapper<Userchecks>();
        userchecksQueryWrapper.eq("userCheckName", name).eq("userId", id).likeRight("userCheckInTime", lastmonth());
        List<Userchecks> userchecks = userChecksMapper.selectList(userchecksQueryWrapper);
        return userchecks;
    }
    /*
     * @Description  获取到当前年份还有月份，获取到当前月份有多少天（调用的方法）。
     * 然后获取当前年份月份
     * 然后通过字符串拼接的形式先拼接下日期，然后存到类型为date的集合里
     * 然后开始判断参数里的集合和这个全部日期的集合是否一致，不一致的就是旷工的日子
     * 接下来返回dete集合
     * @param datecheckinlist:已经打卡的集合
     * @return List<Date>
     * @author liusupeng
     * @date 2020/5/22 10:00
     */
    List<Date> timeday(List<Date> datecheckinlist) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        int day = maxdayInmonth();//获取当前月份多少天
        Calendar calendarday = Calendar.getInstance();
        int year = calendarday.get(Calendar.YEAR);//当前年
        int month = calendarday.get(Calendar.MONTH);//当前月+1 现在测试先不换
        List<Date> datelist = new ArrayList<Date>();
        for (int j = 1; j <= day; j++) {
            Date date = new Date();
            String formart = year + "-" + month + "-" + j;
            datelist.add(sdf.parse(formart));
        }

        //拿到没打卡的时间
        for (int i = 0; i < datelist.size(); i++) {
            for (int j = 0; j < datecheckinlist.size(); j++) {
                if (datelist.get(i).getTime() == datecheckinlist.get(j).getTime()) {
                    datelist.remove(i);
                }
            }
        }

        return datelist;


    }
    /*
     * @Description  获取当前月份有多少天
     * @param
     * @return int
     * @author liusupeng
     * @date 2020/5/22 10:01
     */
    public int maxdayInmonth() {

        Calendar calendarday = Calendar.getInstance();
        int year = calendarday.get(Calendar.YEAR);//当前年
        int month = calendarday.get(Calendar.MONTH);//当前月+1 现在测试先不换
        calendarday.set(year, month, 0);//设置年月
        int day = calendarday.get(Calendar.DAY_OF_MONTH);//获取当前月份有多少天
        return day;

    }
    /*
     * @Description 取到上个月的年月
     * @param
     * @return java.lang.String
     * @author liusupeng
     * @date 2020/6/6 13:50
     */
    public  String lastmonth(){


        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");

        Calendar c=Calendar.getInstance();
        c.set( c.get(Calendar.YEAR),c.get(Calendar.MONTH),0);
        // System.out.println("上个月：>======================"+sdf.format(c.getTime()));
        return sdf.format(c.getTime());

    }

}